

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>10.9. linecache — Random access to text lines &mdash; Python v2.7.8 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7.8',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.7.8 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.7.8 documentation" href="../index.html" />
    <link rel="up" title="10. File and Directory Access" href="filesys.html" />
    <link rel="next" title="10.10. shutil — High-level file operations" href="shutil.html" />
    <link rel="prev" title="10.8. fnmatch — Unix filename pattern matching" href="fnmatch.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="shutil.html" title="10.10. shutil — High-level file operations"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="fnmatch.html" title="10.8. fnmatch — Unix filename pattern matching"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="filesys.html" accesskey="U">10. File and Directory Access</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-linecache">
<span id="linecache-random-access-to-text-lines"></span><h1>10.9. <a class="reference internal" href="#module-linecache" title="linecache: This module provides random access to individual lines from text files."><tt class="xref py py-mod docutils literal"><span class="pre">linecache</span></tt></a> &#8212; Random access to text lines<a class="headerlink" href="#module-linecache" title="Permalink to this headline">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Lib/linecache.py">Lib/linecache.py</a></p>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-linecache" title="linecache: This module provides random access to individual lines from text files."><tt class="xref py py-mod docutils literal"><span class="pre">linecache</span></tt></a> module allows one to get any line from any file, while
attempting to optimize internally, using a cache, the common case where many
lines are read from a single file.  This is used by the <a class="reference internal" href="traceback.html#module-traceback" title="traceback: Print or retrieve a stack traceback."><tt class="xref py py-mod docutils literal"><span class="pre">traceback</span></tt></a> module
to retrieve source lines for inclusion in  the formatted traceback.</p>
<p>The <a class="reference internal" href="#module-linecache" title="linecache: This module provides random access to individual lines from text files."><tt class="xref py py-mod docutils literal"><span class="pre">linecache</span></tt></a> module defines the following functions:</p>
<dl class="function">
<dt id="linecache.getline">
<tt class="descclassname">linecache.</tt><tt class="descname">getline</tt><big>(</big><em>filename</em>, <em>lineno</em><span class="optional">[</span>, <em>module_globals</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#linecache.getline" title="Permalink to this definition">¶</a></dt>
<dd><p>Get line <em>lineno</em> from file named <em>filename</em>. This function will never raise an
exception &#8212; it will return <tt class="docutils literal"><span class="pre">''</span></tt> on errors (the terminating newline character
will be included for lines that are found).</p>
<p id="index-0">If a file named <em>filename</em> is not found, the function will look for it in the
module search path, <tt class="docutils literal"><span class="pre">sys.path</span></tt>, after first checking for a <span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a>
<tt class="docutils literal"><span class="pre">__loader__</span></tt> in <em>module_globals</em>, in case the module was imported from a
zipfile or other non-filesystem import source.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.5: </span>The <em>module_globals</em> parameter was added.</p>
</dd></dl>

<dl class="function">
<dt id="linecache.clearcache">
<tt class="descclassname">linecache.</tt><tt class="descname">clearcache</tt><big>(</big><big>)</big><a class="headerlink" href="#linecache.clearcache" title="Permalink to this definition">¶</a></dt>
<dd><p>Clear the cache.  Use this function if you no longer need lines from files
previously read using <a class="reference internal" href="#linecache.getline" title="linecache.getline"><tt class="xref py py-func docutils literal"><span class="pre">getline()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="linecache.checkcache">
<tt class="descclassname">linecache.</tt><tt class="descname">checkcache</tt><big>(</big><span class="optional">[</span><em>filename</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#linecache.checkcache" title="Permalink to this definition">¶</a></dt>
<dd><p>Check the cache for validity.  Use this function if files in the cache  may have
changed on disk, and you require the updated version.  If <em>filename</em> is omitted,
it will check all the entries in the cache.</p>
</dd></dl>

<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">linecache</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">linecache</span><span class="o">.</span><span class="n">getline</span><span class="p">(</span><span class="s">&#39;/etc/passwd&#39;</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="go">&#39;sys:x:3:3:sys:/dev:/bin/sh\n&#39;</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="fnmatch.html"
                        title="previous chapter">10.8. <tt class="docutils literal docutils literal docutils literal"><span class="pre">fnmatch</span></tt> &#8212; Unix filename pattern matching</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="shutil.html"
                        title="next chapter">10.10. <tt class="docutils literal docutils literal"><span class="pre">shutil</span></tt> &#8212; High-level file operations</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/linecache.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="shutil.html" title="10.10. shutil — High-level file operations"
             >next</a> |</li>
        <li class="right" >
          <a href="fnmatch.html" title="10.8. fnmatch — Unix filename pattern matching"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="filesys.html" >10. File and Directory Access</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2014, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jun 29, 2014.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>